package ucap.com.lesanapp.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;


import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import butterknife.Bind;
import butterknife.ButterKnife;
import ucap.com.lesanapp.R;
import ucap.com.lesanapp.base.BaseDialog;
import ucap.com.lesanapp.bean.NewContentData;
import ucap.com.lesanapp.bean.NewsDetailedBean;
import ucap.com.lesanapp.global.Constants;
import ucap.com.lesanapp.global.SettingsModelState;

/**
 * Created by Administrator on 2016/5/9.
 */
public class WebViewDialog extends BaseDialog {


    @Bind(R.id.web_dialog)
    WebView webView;
    @Bind(R.id.pb_loading)
    ProgressBar pbLoading;
    private String code;
    private String head;
    private String cid;

    protected WebViewDialog(Context context, String code, String head, String cid) {
        super(context);
        this.code = code;
        this.head = head;
        this.cid = cid;
    }

    public static void showDialog(Context context, String url, String head, String cid) {
        WebViewDialog listViewDialog = new WebViewDialog(context, url, head, cid);
        listViewDialog.show();
    }

    @Override
    public void setView() {
        setContentView(R.layout.my_dialog_layout);
        ButterKnife.bind(this);
        pbLoading.setVisibility(View.VISIBLE);
        webView.setVisibility(View.INVISIBLE);
    }

    @Override
    public void setListener() {

    }

    public void getDateForService() {
        HttpUtils utils = new HttpUtils();
        utils.send(HttpMethod.GET, Constants.URL_CONTENT + "?manuscriptid=" + code + "&&cid=" + cid, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                analysisDate(responseInfo.result);
                Log.i("myDialogDate", responseInfo.result);
            }

            public void onFailure(HttpException error, String msg) {
            }
        });
    }

    private void analysisDate(String result) {
        Gson gson = new Gson();
        NewContentData newContentData = gson.fromJson(result, NewContentData.class);
        WebSettings setting = webView.getSettings();
        setting.setJavaScriptEnabled(true);
        setting.setSupportZoom(false);
        setting.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        setting.setRenderPriority(WebSettings.RenderPriority.HIGH); //提高渲染速度
        setting.setBlockNetworkImage(true);
        setting.setUseWideViewPort(true);//关键点
        setting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        setting.setLoadWithOverviewMode(true);
        setting.setBlockNetworkImage(true);//别显示图片视频
        webView.loadUrl(newContentData.getUrl());
        webView.setWebViewClient(new WebViewClient() {
            //网页开始加载
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                Log.i("webView", "网页开始加载");
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Log.i("webView", "网页跳转");
                view.loadUrl(url);
                return true;

            }

            @Override
            public void onPageFinished(WebView view, String url) {
                Log.i("webView", "网页加载结束");
                pbLoading.setVisibility(View.INVISIBLE);
                webView.setVisibility(View.VISIBLE);
            }
        });


    }

    @Override
    public void setData() {
        getDateForService();
    }

    @Override
    public void setClick(View v) {

    }
}
